#include "mainwindow.h"
#include "texteditor.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QWidget>
#include <QSplitter>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setupUI();
    setWindowTitle("富文本编辑器 - 三栏布局");
    resize(1400, 800);
}

MainWindow::~MainWindow()
{
}

void MainWindow::setupUI()
{
    // 创建中央窗口部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建水平分割器，用于三栏布局
    QSplitter *mainSplitter = new QSplitter(Qt::Horizontal, centralWidget);

    // 设置分割器样式
    mainSplitter->setStyleSheet(
        "QSplitter::handle {"
        "    background-color: #e0e0e0;"
        "    width: 3px;"
        "}"
        "QSplitter::handle:hover {"
        "    background-color: #0078d4;"
        "}"
    );

    // 创建左侧目录面板
    setupCatalogPanel(mainSplitter);

    // 创建中间的文本编辑器
    setupTextEditor(mainSplitter);

    // 创建右侧注释面板
    setupCommentPanel(mainSplitter);

    // 设置各部分的初始大小比例 (1:4:1)
    mainSplitter->setSizes({200, 800, 200});

    // 设置最小宽度
    mainSplitter->widget(0)->setMinimumWidth(150); // 目录最小宽度
    mainSplitter->widget(1)->setMinimumWidth(400); // 编辑器最小宽度
    mainSplitter->widget(2)->setMinimumWidth(150); // 注释最小宽度

    // 创建主布局
    QHBoxLayout *mainLayout = new QHBoxLayout(centralWidget);
    mainLayout->setContentsMargins(5, 5, 5, 5);
    mainLayout->addWidget(mainSplitter);

    centralWidget->setLayout(mainLayout);
}

void MainWindow::setupCatalogPanel(QSplitter *parent)
{
    // 创建左侧目录容器
    QWidget *catalogWidget = new QWidget(parent);
    catalogWidget->setStyleSheet(
        "QWidget {"
        "    background-color: #f8f9fa;"
        "    border: 1px solid #dee2e6;"
        "    border-radius: 4px;"
        "}"
    );

    QVBoxLayout *catalogLayout = new QVBoxLayout(catalogWidget);
    catalogLayout->setContentsMargins(10, 10, 10, 10);
    catalogLayout->setSpacing(8);

    // 创建目录标签
    catalogLabel = new QLabel("目录", catalogWidget);
    catalogLabel->setStyleSheet(
        "QLabel {"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    color: #333;"
        "    padding: 8px 0;"
        "    border: none;"
        "    background-color: transparent;"
        "    border-bottom: 2px solid #0078d4;"
        "}"
    );
    catalogLabel->setAlignment(Qt::AlignCenter);

    // 创建目录文本编辑器
    catalogTextEdit = new QTextEdit(catalogWidget);
    catalogTextEdit->setPlaceholderText("在此输入文档目录...");
    catalogTextEdit->setStyleSheet(
        "QTextEdit {"
        "    border: 1px solid #ced4da;"
        "    border-radius: 4px;"
        "    background-color: white;"
        "    font-size: 12px;"
        "    font-family: '微软雅黑', sans-serif;"
        "    padding: 8px;"
        "    line-height: 1.4;"
        "}"
        "QTextEdit:focus {"
        "    border-color: #0078d4;"
        "    outline: none;"
        "}"
        "QTextEdit:hover {"
        "    border-color: #a0a6ac;"
        "}"
    );

    // 设置目录文本编辑器的默认内容
    catalogTextEdit->setPlainText(
        "1. 引言\n"
        "2. 主要内容\n"
        "   2.1 第一部分\n"
        "   2.2 第二部分\n"
        "   2.3 第三部分\n"
        "3. 总结\n"
        "4. 参考文献"
    );

    // 将控件添加到布局
    catalogLayout->addWidget(catalogLabel);
    catalogLayout->addWidget(catalogTextEdit);

    // 添加到分割器
    parent->addWidget(catalogWidget);
}

void MainWindow::setupTextEditor(QSplitter *parent)
{
    // 创建文本编辑器实例
    textEditor = new TextEditor(parent);

    // 设置文本编辑器的样式
    textEditor->setStyleSheet(
        "TextEditor {"
        "    border: 1px solid #dee2e6;"
        "    border-radius: 4px;"
        "    background-color: white;"
        "}"
    );

    // 添加到分割器
    parent->addWidget(textEditor);
}

void MainWindow::setupCommentPanel(QSplitter *parent)
{
    // 创建右侧注释容器
    QWidget *commentWidget = new QWidget(parent);
    commentWidget->setStyleSheet(
        "QWidget {"
        "    background-color: #f8f9fa;"
        "    border: 1px solid #dee2e6;"
        "    border-radius: 4px;"
        "}"
    );

    QVBoxLayout *commentLayout = new QVBoxLayout(commentWidget);
    commentLayout->setContentsMargins(10, 10, 10, 10);
    commentLayout->setSpacing(8);

    // 创建注释标签
    commentLabel = new QLabel("注释", commentWidget);
    commentLabel->setStyleSheet(
        "QLabel {"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    color: #333;"
        "    padding: 8px 0;"
        "    border: none;"
        "    background-color: transparent;"
        "    border-bottom: 2px solid #28a745;"
        "}"
    );
    commentLabel->setAlignment(Qt::AlignCenter);

    // 创建注释文本编辑器
    commentTextEdit = new QTextEdit(commentWidget);
    commentTextEdit->setPlaceholderText("在此输入注释和备注...");
    commentTextEdit->setStyleSheet(
        "QTextEdit {"
        "    border: 1px solid #ced4da;"
        "    border-radius: 4px;"
        "    background-color: white;"
        "    font-size: 12px;"
        "    font-family: '微软雅黑', sans-serif;"
        "    padding: 8px;"
        "    line-height: 1.4;"
        "}"
        "QTextEdit:focus {"
        "    border-color: #28a745;"
        "    outline: none;"
        "}"
        "QTextEdit:hover {"
        "    border-color: #a0a6ac;"
        "}"
    );

    // 设置注释文本编辑器的默认内容
    commentTextEdit->setPlainText(
        "• 记录要点\n"
        "• 添加提醒\n"
        "• 保存想法\n"
        "\n"
        "这里可以记录：\n"
        "- 重要提醒\n"
        "- 待办事项\n"
        "- 创作灵感"
    );

    // 将控件添加到布局
    commentLayout->addWidget(commentLabel);
    commentLayout->addWidget(commentTextEdit);

    // 添加到分割器
    parent->addWidget(commentWidget);
}

// 获取目录内容
QString MainWindow::getCatalogContent() const
{
    return catalogTextEdit ? catalogTextEdit->toPlainText() : QString();
}

// 设置目录内容
void MainWindow::setCatalogContent(const QString &content)
{
    if (catalogTextEdit) {
        catalogTextEdit->setPlainText(content);
    }
}

// 获取注释内容
QString MainWindow::getCommentContent() const
{
    return commentTextEdit ? commentTextEdit->toPlainText() : QString();
}

// 设置注释内容
void MainWindow::setCommentContent(const QString &content)
{
    if (commentTextEdit) {
        commentTextEdit->setPlainText(content);
    }
}

// 获取文本编辑器实例
TextEditor* MainWindow::getTextEditor() const
{
    return textEditor;
}
